package app.util.image.action.morph
{
	import app.util.*;
	import app.util.image.*;
	import app.util.image.action.*;
	import app.util.image.calc.*;
	
	import flash.display.*;

	public class EdgeDetectAction extends SEBasedAction
	{
		private var progress:Number = 0;
		private var p1:BinaryBitmapData;
		private var p2:BinaryBitmapData;
		private var ret:BinaryBitmapData;
		private var src:BinaryBitmapData;
		private var mode:int;	//0:S, 1:I, 2:E
		
		public function EdgeDetectAction(mode:int)
		{
			this.mode = mode;	
		}
		
		public override function getDescription():String
		{
			switch(this.mode)
			{
				case 0:
					return "Standard Edge";
				case 1:
					return "Internal Edge";
				case 2:
					return "External Edge";	
			}
			
			throw new Error("Unknown edge detection mode");
		}
		
		protected override function getFinalBitmapData():BitmapData
		{
			return this.ret.toBitmapData();
		}
		
		protected override function processBitmapDataByStep():Number
		{
			if(this.progress == 0)
			{
				this.src = MorphUtility.autoThreshold(Mediator.getCurrentBitmapData());
				this.p1 = MorphUtility.doDilation(src, se);
				this.progress += 0.25;
			}
			else if(this.progress == 0.25)
			{
				this.p2 = MorphUtility.doErosion(src, se);
				this.progress += 0.25;
			}
			else
			{
				switch(this.mode)
				{
					case 0:
						this.ret = this.p1.minus(p2);
					case 1:
						this.ret = this.src.minus(p2);
					case 2:
						this.ret = this.p1.minus(src);
				}
				this.progress += 0.5;		
			}
			
			return this.progress;
		}
	}
}